function Wages_Code() {
	var buffer = new StringBuffer();
	var date=new Date();
	buffer.append('<div id="find" class="finds"><div class="searchinput ui-border-radius"><input id="keyword" class="bordersolid" type="text" value="" onblur="Show_Search()" onkeyup="Wages_Search();"/></div><div id="searchicon" class="searchicon"><img src="img/jing.jpg"/></div><div id="searchicons" class="searchicons"><img src="img/jings.jpg"/></div><div class="screen"><button id="screen" class="ui-border-radius" year="'+date.getFullYear()+'" month="'+(date.getMonth()+1)+'">'+(date.getMonth()+1)+'月</button></div></div>');
	buffer.append('<div class="horizen border-bottom"></div>');
	return buffer.toString();
};
function Wages_Data(data) {
	var buffer = new StringBuffer();
	$(data).each(function(i,info){
		buffer.append('<div class="crmsheet border-bottom date" id="'+info.ID+'" realname="'+info.RealName+'" bsalary="'+info.BSalary+'"><div class="ctitle" style="width:40%;"><div class="cimg"><img src="'+info.HeadImg+'"/></div><div class="cname">&nbsp;&nbsp;'+info.RealName+'</div></div><div class="ctext '+(info.HasWaged?"grays":"greens")+'" style="width:50%;padding-top:5px; text-align:right;">'+info.Wages.toFixed(2)+'</div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	});
	return buffer.toString();
};
function Init_Wages_Code()
{
	get("find").addEventListener("tap",function(){
		$("#searchicon").hide();
	    $("#searchicons").show();
	    $("#keyword").attr("placeholder","姓名、用户名、电话");
		if($("keyword").val()!="")
		{
			//WorkMate_Search();
		}
	});
	
	get("screen").addEventListener("tap",function(){
		Show_Month(function(year,month){
			$("#screen").attr("year",year).attr("month",month).html(month+"月");
			Wages_Search();
		});
		stopEvent();
	});
};
function Wages_Search(){
	pageindex=1;
	$("#divmsg1").attr("pageindex","0");
	Receive_Wages(1);
};
function Receive_Wages(pageindex)
{
	var isenable=RUndefined($("input[name='state_search']:checked").val());
	var departid=RUndefined($("#depart_search").val());
	var data = {};
			data.act = "Find_Wages";
			data.userid=userid;
			data.comid=comid;
			data.pageindex=pageindex;
			data.source=source;
			data.pagesize=PAGE_SIZE20;
			data.keyword = $("#keyword").val();
    		data.year=RUndefined($("#screen").attr("year"));
			data.month=RUndefined($("#screen").attr("month"));
    		data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				if(json.data.user.length>0)
				{
					if(pageindex==1)
					{
						$("#contentlist1").html(Wages_Data(json.data.user));
					}else{
						$("#contentlist1").append(Wages_Data(json.data.user));
					}
					setTimeout(function(){
						Init_Wages();
						myScroll1.refresh();
					},500); //需要延迟500毫秒加载，否则当数量少时不能下拉
					if(json.data.user.length!=data.pagesize)
					{
						$("#loading1").html("");
					}else{
						$("#loading1").html('<img src="img/loading.gif"/>');
					}
				}else
				{
					
					if(pageindex==1)
					{
						$("#contentlist1").html("");
						$("#loading1").html("主人，没有找到您需要的数据"); 
					}
					else
					{
						$("#loading1").html("没有更多了");
					}
				}
				
         		pageindex = parseInt($("#divmsg1").attr("pageindex"))+1;
         		$("#divmsg1").attr("pageindex",pageindex);
			});
};

function Init_Wages()
{
	mui("#contentlist1").off("tap",".crmsheet");
	mui("#contentlist1").on("tap",".crmsheet",function(){
		obj=$(this);
		OpenWindow(2,obj.attr("realname")+"的"+RUndefined($("#screen").attr("month"))+"月工资详细","保存",function(){
			Save_Wages();
		});
		
		Init_WagesAdd(obj.attr("id"));
	});
};

function WagesAdd_Code(id,bsalary){
	var buffer = new StringBuffer();
	buffer.append('<div id="wagesid" val="'+id+'" class="crmsheet border-bottom"><div class="ctitle"><div class="cname">基本工资</div></div><div class="ctext" style="width:40%;"><input id="txt_bsalary" class="greens" type="tel" reward="1" value="'+bsalary.toFixed(2)+'" readonly="readonly"/></div><div class="carrow" style="color:#a9a9a9;font-size:12px;width:22%; text-align:right; padding-right:10px;">不可修改</div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">迟到罚款</div></div><div class="ctext"><input id="txt_chidao" class="reds" type="tel" reward="2" placeholder="填写迟到罚款" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">早退罚款</div><div class="require"></div></div><div class="ctext"><input id="txt_zaotui" class="reds" type="tel" reward="2" placeholder="填写早退罚款" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">加班补贴</div></div><div class="ctext"><input id="txt_jiaban" class="greens" type="tel" reward="1" placeholder="填写加班补贴" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">业绩提成</div></div><div class="ctext"><input id="txt_yeji" class="greens" type="tel" reward="1" placeholder="填写业绩提成" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">全勤奖</div></div><div class="ctext"><input id="txt_quanqin" class="greens" type="tel" reward="1" placeholder="填写全勤奖" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">工龄奖</div></div><div class="ctext"><input id="txt_gongling" class="greens" type="tel" reward="1" placeholder="填写工龄奖" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">餐费补贴</div></div><div class="ctext"><input id="txt_canfei" class="greens" type="tel" reward="1" placeholder="填写餐费补贴" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">住房补贴</div></div><div class="ctext"><input id="txt_zhufang" class="greens" type="tel" reward="1" placeholder="填写住房补贴" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">交通补贴</div></div><div class="ctext"><input id="txt_jiaotong" class="greens" type="tel" reward="1" placeholder="填写交通补贴" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">电话补贴</div></div><div class="ctext"><input id="txt_dianhua" class="greens" type="tel" reward="1" placeholder="填写电话补贴" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	buffer.append('<div class="crmsheet border-bottom hidden"><div class="ctitle"><div class="cname">社保扣款</div></div><div class="ctext"><input id="txt_shebao" class="reds" type="tel" reward="2" placeholder="填写社保扣款" value="0" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	return buffer.toString();
};

function RemoveWages(o){
	$(o).parent().parent().addClass("hidden").find("input").val(0.00);
	ReCalWages();
};

function ReCalWages(){
	var money=0;
	$("#contentlist2 input").each(function(i,info){
		var cname=$(info).parent().parent().find(".cname").html();
		if(cname!="个税" && cname!="总工资")
		{
			var val=0;
			if(info.value=="" || isNaN(info.value))
			{
				val=0;
			}else
			{
				val=parseFloat(info.value);
			}
			
			if($(info).attr("reward")=="1"){
				money+=val;
			}else{
				money-=val;
			}
		}
		if(cname=="个税" && !$("#txt_geshui").parent().parent().hasClass("hidden"))
		{
			var tax=CalTax(money);
			$("#txt_geshui").val(tax.toFixed(2));
			money-=tax;
		}
	});
	$("#txt_allmoney").val(money.toFixed(2));
};

function Init_WagesAdd(uid){
		var data = {};
		data.act = "Find_OneWages";
		data.id=userid;
		data.source=source;
		data.userid=uid;
		data.comid=comid;
		data.year=RUndefined($("#screen").attr("year"));
		data.month=RUndefined($("#screen").attr("month"));
		data.sign=GetSign(data.act); 
		mui.getJSON(index_domain, data, function (json) {
		    if (json.state) {
		    	var info=json.data.wages;
		    	$("#contentlist2").html(WagesAdd_Code(info.ID,obj.attr("bsalary")));
		    	var allmoney=parseFloat($("#txt_bsalary").val())-info.ChiDao-info.ZaoTui+info.JiaBan+info.YeJi+info.QuanQin+info.GongLing+info.CanFei+info.ZhuFang+info.JiaoTong+info.DianHua;
		    	if(info.ChiDao>0){$("#txt_chidao").val(info.ChiDao.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.ZaoTui>0){$("#txt_zaotui").val(info.ZaoTui.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.JiaBan>0){$("#txt_jiaban").val(info.JiaBan.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.YeJi>0){$("#txt_yeji").val(info.YeJi.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.QuanQin>0){$("#txt_quanqin").val(info.QuanQin.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.GongLing>0){$("#txt_gongling").val(info.GongLing.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.CanFei>0){$("#txt_canfei").val(info.CanFei.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.ZhuFang>0){$("#txt_zhufang").val(info.ZhuFang.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.JiaoTong>0){$("#txt_jiaotong").val(info.JiaoTong.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.DianHua>0){$("#txt_dianhua").val(info.DianHua.toFixed(2)).parent().parent().removeClass("hidden");}
		    	if(info.SheBao>-1){$("#txt_shebao").val(info.SheBao.toFixed(2)).parent().parent().removeClass("hidden"); allmoney -=info.SheBao;}
		    	if(json.data.flag=="add"){
			    	if(json.data.reward.length>0)
			    	{
						$.each(json.data.reward,function(i,reward){
							if(reward.Money>0){
								if(reward.Type==1){allmoney+=reward.Money}else{allmoney-=reward.Money}
								$("#contentlist2").append('<div class="crmsheet border-bottom selfreward" val="'+reward.ColumnNum+'"><div class="ctitle"><div class="cname">'+reward.Name+'</div></div><div class="ctext"><input id="txt_shebao" class="'+(reward.Type==1?"greens":"reds")+'" type="tel" reward="'+reward.Type+'" placeholder="填写'+reward.Name+'" value="'+reward.Money.toFixed(2)+'" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
							}
						});
			    	}
			    	var tax=CalTax(allmoney);

			    	$("#contentlist2").append('<div id="selfrewardoption"></div>');
			    	$("#contentlist2").append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">个税</div></div><div class="ctext"><input id="txt_geshui" class="reds" type="tel" reward="2" placeholder="" value="'+tax.toFixed(2)+'" readonly="readonly"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
			    	$("#contentlist2").append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">总工资</div></div><div class="ctext"><input id="txt_allmoney" class="greens" type="tel" placeholder="" value="'+(allmoney-tax).toFixed(2)+'" readonly="readonly"/></div><div class="carrow"></div></div>');
		    	}else{
		    		var selfreward1=info.SelfReward1.split(',');
		    		var selfreward2=info.SelfReward2.split(',');
		    		var str="";
		    		$.each(selfreward1,function(i,reward){
		    				reward=reward.split('*');
							if(parseFloat(reward[2])>0){
								$("#contentlist2").append('<div class="crmsheet border-bottom selfreward"><div class="ctitle"><div class="cname">'+reward[0]+'</div></div><div class="ctext"><input id="txt_shebao" class="'+(reward[1]=="1"?"greens":"reds")+'" type="tel" reward="'+reward[1]+'" placeholder="填写'+reward[0]+'" value="'+reward[2].toFixed(2)+'" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
							}
					});
					$.each(selfreward2,function(i,reward){
							reward=reward.split('*');
							if(parseFloat(reward[2])>0){
								str+='<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">'+reward[0]+'</div></div><div class="ctext"><input id="txt_shebao" class="'+(reward[1]=="1"?"greens":"reds")+'" type="tel" reward="'+reward[1]+'" placeholder="填写'+reward[0]+'" value="'+reward[2].toFixed(2)+'" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>';
							}
					});
					$("#contentlist2").append('<div id="selfrewardoption">'+str+'</div>');
			    	$("#contentlist2").append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">个税</div></div><div class="ctext"><input id="txt_geshui" class="reds" type="tel" reward="2" placeholder="" value="'+info.GeShui.toFixed(2)+'" readonly="readonly"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
			    	$("#contentlist2").append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">总工资</div></div><div class="ctext"><input id="txt_allmoney" class="greens" type="tel" placeholder="" value="'+info.AllMoney.toFixed(2)+'" readonly="readonly"/></div><div class="carrow"></div></div>');
		    	}
		    	$("#contentlist2").append('<div id="addotherwages" class="crmsheet border-bottom addvote">添加其他奖励或罚款</div>');
		    	get("addotherwages").addEventListener("tap",function(){
		    		OpenWindow(3,"添加其他奖励或罚款","添加",function(){
						Save_WagesOption();
					});
					$("#contentlist3").html(WagesOptionAdd_Code());
		    	});
		    }
		});
};

function WagesOptionAdd_Code(){
	var buffer = new StringBuffer();
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">奖罚名称</div><div class="require">*</div></div><div class="ctext"><input id="txt_rewardname" type="text" placeholder="例如：月度优秀员工奖" value="" /></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">奖励/罚款</div><div class="require">*</div></div><div class="ctext"><div class="sex"><input id="boy" name="txt_rewardtype" type="radio" style="width:16px;" checked="checked" value="1" onclick="Change_Salary()"/><label for="boy">奖励</label></div><div class="sex"><input id="girls" name="txt_rewardtype" type="radio" style="width:16px;" value="2" onclick="Change_Salary()"/><label for="girls">罚款</label></div></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">奖罚金额</div><div class="require">*</div></div><div class="ctext"><input id="txt_rewardmoney" type="tel" placeholder="填写奖励或罚款金额" value="" onkeyup="Change_Salary();"/></div><div class="carrow"></div></div>');
	return buffer.toString();
};
function Save_WagesOption(){
	var rewardname=$("#txt_rewardname");
	var rewardmoney=$("#txt_rewardmoney");
	if(rewardname.val().Trim()=="")
    {
        plus.nativeUI.toast("请填写奖罚名称");
        return false;
    }
    if(rewardmoney.val().Trim()=="")
    {
        plus.nativeUI.toast("请填写奖罚金额");
        return false;
    }
    var rewardtype=$("input[name='txt_rewardtype']:checked").val();
	$("#selfrewardoption").append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">'+rewardname.val()+'</div></div><div class="ctext"><input class="'+(rewardtype==1?"greens":"reds")+'" type="tel" reward="'+rewardtype+'" placeholder="填写'+rewardname.val()+'" value="'+rewardmoney.val().toFixed(2)+'" onkeyup="ReCalWages()"/></div><div class="carrow" style="margin-top:3px;"><a onclick="RemoveWages(this)">×</a></div></div>');
	ReCalWages();
	CloseWindow();
};


function Save_Wages(){
		
		var data = {};
        data.id = $("#wagesid").attr("val");
		data.act = "Save_Wages";
		data.uid=obj.attr("id");
		data.year=RUndefined($("#screen").attr("year"));
		data.month=RUndefined($("#screen").attr("month"));
		data.bsalary=$("#txt_bsalary").val();
		data.chidao = $("#txt_chidao").val();
        data.zaotui = $("#txt_zaotui").val();
        data.jiaban=$("#txt_jiaban").val();
        data.yeji = $("#txt_yeji").val();
        data.quanqin = $("#txt_quanqin").val();
        data.gongling = $("#txt_gongling").val();
        data.canfei = $("#txt_canfei").val();
        data.zhufang = $("#txt_zhufang").val();
        data.jiaotong = $("#txt_jiaotong").val();
        data.dianhua = $("#txt_dianhua").val();
        data.shebao = $("#txt_shebao").val();
        data.geshui = $("#txt_geshui").val();
        if(!$("#txt_shebao").parent().parent().hasClass("hidden") && parseInt(data.shebao)==0)
        {
			plus.nativeUI.toast("请先缴纳社保");
			return;
        }
        data.allmoney=$("#txt_allmoney").val();
        data.selfreward1="";
        $("#contentlist2 .selfreward").each(function(i,info){
        	if(parseFloat($(info).find("input").val())>0){
        		data.selfreward1+=$(info).find(".cname").html()+"*"+$(info).find("input").attr("reward")+"*"+$(info).find("input").val()+",";
        	}
        });
        data.selfreward2="";
        $("#selfrewardoption .crmsheet").each(function(i,info){
        	if(parseFloat($(info).find("input").val())>0){
        		data.selfreward2+=$(info).find(".cname").html()+"*"+$(info).find("input").attr("reward")+"*"+$(info).find("input").val()+",";
        	}
        });
        data.userid=userid;
		data.comid=comid;
        data.source = source;
        data.version = version;
        data.sign=GetSign(data.act);
        plus.nativeUI.showWaiting( "正在保存..." );
	    CloseWaiting(10000);
        mui.getJSON(index_domain, data, function (json) {
        	plus.nativeUI.closeWaiting();
        	plus.nativeUI.toast(json.msg);
            if (json.state) {
                CloseWindow(); 
                obj.find(".ctext").html(data.allmoney).attr("class","ctext grays");
            }
        });
};
